#coding=utf-8

from db_manager import DBManager 
from  config import *
import datetime
from tool import Tool
class Shop(object):
    
    def __init__(self,cfgParam):
        self.cfgParam = cfgParam
        self.manager = DBManager(db,ip,ur,pd)
        self.manager.connect()
    
        self.tool = Tool()
    def query_product(self):
        
        
        mustkeys = ["merchantid"]
        ret = self.tool.check_param(mustkeys,self.cfgParam) 
        if not ret["status"]:
            return ret

        try: 
            condition = {}
            result = self.manager.smartQuery(product_info,condition)
	    for row in result:
	        row["detail_picurl"] = row["detail_picurl"].split(",")

            resultdic = {}
        except Exception,e:
            print e
	ret["result"] = result
	return ret

    def add_shopcart(self):
        
        mustkeys = ["merchantid","productid"]
        ret = self.tool.check_param(mustkeys,self.cfgParam) 
        if not ret["status"]:
            return ret
        merchantid = int(self.cfgParam["merchantid"])
	productid = int(self.cfgParam["productid"])
	count     = int(self.cfgParam["count"])
	ordertime=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        condition = {'productid':productid,"merchantid":merchantid,"count":count,"ordertime":ordertime}
        
        result = self.manager.smartInsert(product_order,condition)
	ret["result"] = result
	return ret


    def query_order(self):
        mustkeys = ["merchantid"]
        ret = self.tool.check_param(mustkeys,self.cfgParam) 
        if not ret["status"]:
            return ret
        merchantid = int(self.cfgParam["merchantid"])
        
	qrysql = "select b.name,b.description,b.picurl,b.price,a.orderid,a.productid,a.count,a.ordertime,a.status from %s as a inner join %s as b on a.productid = b.productid where a.status = 0" %(product_order,product_info)
	result = self.manager.query(qrysql)
        resultdic = {}
	ret["result"] = result
	return ret

if __name__ == '__main__':
    s=Shop('1')









